package edu.mit.media.fv.components
{
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.filters.*;
	import flash.geom.Rectangle;
	import flash.text.*;
	
	public class FVHorizontalSlider extends Sprite
	{
		private var _thumbGirth:int = 35;
		private var _width:int = 0;
		private var _height:int = 0;
		private var _thumb:Sprite;
		private var _track:Sprite;
		private var _bounds:Rectangle;
		
		public function FVHorizontalSlider(x:int, y:int, width:int, height:int)
		{
			super();
			
			this._track = new Sprite();
			//var f:GradientBevelFilter = new GradientBevelFilter();
			var f:BevelFilter = new BevelFilter(4, 45,16777215,1,0x262626,1.0);
			this._track.filters = [f];
			this.addChild(this._track);
			
			this._thumb = new Sprite();
			this.addChild(this._thumb);
			
			this.setBounds(x, y, width, height);
			
			this._thumb.addEventListener(MouseEvent.MOUSE_DOWN, moveThumb);
			this.addEventListener(MouseEvent.MOUSE_UP, dropThumb);
			this.addEventListener(MouseEvent.ROLL_OUT, dropThumb);
			
		}
		public function setBounds(x:int, y:int, width:int, height:int):void{
			this.x = x;
			this.y = y;
			this._width = width;
			this._height = height;
			this._bounds = new Rectangle(0,0,width-this._thumbGirth*1.5, 0);
			this.reformThumb();
			this.reformTrack();
			//hit zone
			this.graphics.clear();
			this.graphics.beginFill(0xFFFFFF, 0.0);
			this.graphics.drawRect(0,0,width, height);
			this.graphics.endFill();
		}
		public function get value():Number{
			return this._thumb.x/this._width;
		}
		public function set value(v:Number):void{
			this._thumb.x = this._width*v;
		}
		private function moveThumb(event:MouseEvent):void{
			this._thumb.startDrag(false, _bounds);
		}
		private function dropThumb(event:MouseEvent):void{
			this._thumb.stopDrag();
		}
		
		private function reformThumb():void{
			this._thumb.graphics.clear();
			this._thumb.graphics.beginFill(0xFFFFFF, 0.12);
			this._thumb.graphics.lineStyle(1, 0xFFFFFF, 0.70, true);
			this._thumb.graphics.drawRoundRect(0,0,this._thumbGirth,this._height, 10,10); 
			this._thumb.graphics.beginFill(0x046380, 0.5);
			this._thumb.graphics.lineStyle(1, 0xFFFFFF, 0.25, true);
			this._thumb.graphics.drawRoundRect(this._thumbGirth/2-5, 5, 10, this._height-10, 3, 3);
			this._thumb.graphics.endFill();
		}
		
		private function reformTrack():void{
			this._track.graphics.clear();
			this._track.graphics.beginFill(0xEFECCA, 1.0);
			this._track.graphics.lineStyle(1, 0xFFFFFF, 0.7, true);
			this._track.graphics.drawRoundRect(0, this._height/2-5, this._width, 10, 3, 3);
			this._track.graphics.endFill();
		}

	}
}